package com.yonyou.crm.bpub.topbar.appctrl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.yonyou.crm.bpub.topbar.entity.*;
import com.yonyou.crm.bpub.topbar.rmitf.ITopbarRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.*;
import com.yonyou.crm.common.security.util.Md5CodeUtil;


@RequestMapping("/{version}/*/{module}/view/topbars")
@RestController
public class AppTopbarsCtrl{
	
	@Autowired
	ITopbarRmService service;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public Object getTopbar(@PathVariable("version") String version, @PathVariable("module") String module,HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String appversion = paramObj.getString("version");
		Map<String, Object> list = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		
		TopbarVO result = service.getTopbar(module);
		
		data.put("topBarData", result);

		String hashCodeVersion = Md5CodeUtil.createMd5CodeForMap(data);
		
		if (StringUtils.isBlank(appversion) || (!appversion.equals(hashCodeVersion))) {
			list = data;
		}
		
		list.put("version", hashCodeVersion);

		return ResponseUtil.getObject(list);
	}

}